#include <iostream>
#include <memory>
#include "Socket.hpp"
#include "Protocol.hpp"

using namespace SocketModule;

int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        std::cerr << "正确格式: ./tcpClient ip port" << std::endl;
        return 1;
    }

    std::string server_ip = argv[1];
    uint16_t server_port = std::stoi(argv[2]);

    std::shared_ptr<Socket> client = std::make_shared<TcpSocket>();
    client->buildTcpClientSocketMethod();

    if (client->connect(server_ip, server_port) == 0)
    {
        int x = 0;
        int y = 0;
        char oper = 0;
        while (true)
        {
            std::cout << "请输入算式:>" << std::endl;
            std::cin >> x >> oper >> y;

            std::string send_info = Protocol::buildRequestProtocolString(x, y, oper);
            client->send(send_info);

            Response res;
            bool ok = Protocol::getResponse(client, res);
            if (ok == true)
            {
                std::cout << "结果: " << res.getResult() << " 返回值: " << res.getInfoCode() << std::endl;
            }
        }
    }
    else
    {
        LOG(LogLevel::DEBUG) << "connect failure";
    }

    client->close();
    
    return 0;
}